﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeGet;
using WeAppsCenter.Core;

namespace WeAppsCenter;
public static class Installation
{
    public static bool GetIfInstalled(string PackageName, Channel channel)
    {

        string channelString = channel.ToString().ToLower();
        if (Directory.Exists(Settings.DefaultDirectory + $"\\{PackageName}-{channelString}"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static string[] GetInstalledApps()
    {
        string[] directories = Directory.GetDirectories(Settings.DefaultDirectory, "*", SearchOption.TopDirectoryOnly);
        //不定长度数组示例(List)
        var values = new List<string>();
        foreach (string directory in directories)
        {
            //枚举遍历示例
            foreach (var item in Enum.GetValues(typeof(Channel)))
            {
                string channelString = Enum.GetName(typeof(Channel), item).ToLower();
                string final;
                if (directory.Contains(channelString))
                {
                    final = directory.Replace($"-{channelString}", "");
                }
                else
                {
                    break;
                }

                final = final.Replace(Settings.DefaultDirectory, "");
                final = final.Replace("\\", "");
                values.Add(final);
            }
        }
        return values.ToArray();
    }

    public static Channel[] GetInstalledChannels(string PackageName)
    {
        string channelString;
        var values = new List<Channel>();
        foreach (var item in Enum.GetValues(typeof(Channel)))
        {
            channelString = Enum.GetName(typeof(Channel), item).ToLower();
            string directory = Settings.DefaultDirectory + $"\\{PackageName}-{channelString}";
            if (Directory.Exists(directory))
            {
                values.Add((Channel)item);
            }
        }
        return  values.ToArray();
    }
}
